// import { Toast } from 'vant';
import axios from 'axios'
// import store from '@/store/index.js'
import { ElMessage } from 'element-plus'
import router from '@/router/index.js'
const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // baseURL:'http://192.168.1.76:8888',//本地接口
    // baseURL:'https://www.fastmock.site/mock/42b202295dd5f9baa8185b859450ef73/activity',//mock接口
    baseURL:'https://dfhd.guwengkj.com',//正式接口
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 40000 // request timeout
})
// 请求拦截
service.interceptors.request.use(
    config => {
        // do something before request is sent
        const token = localStorage.getItem('TOKEN');
        if (token) {
            config.headers['Authorization'] = 'Bearer '+token
        }
        if (!config.headers['Content-Type']) {
            config.headers['Content-Type'] = 'application/json'
        }
        // config.headers['Content-Type'] = 'application/json'
        // console.log(config)
        // console.log(process.env.NODE_ENV)
        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)
//请求结果拦截
service.interceptors.response.use(res=>{
    return res.data
},error=>{
    if (error.message === 'Network Error') {
        // Toast.fail('服务器连接异常，请检查服务器！');
        return
    }
    if(error.response.data.code == 401) {
        ElMessage({
            message: error.response.data.message,
            type: 'warning',
         })
        router.replace('/login')
    }
    console.log(error) // for debug
    // Toast.fail(error.response.data.message);
    // ElMessage({
    //     message: error.response.data.message,
    //     type: 'warning',
    //  })
    // router.replace('/login') // 跳转登录页
    // return Promise.reject(error)
    return error;
})
export default service
